package com.tcpdump.factory;

import java.io.IOException;

import com.tcpdump.service.TerminalService;

public class RootProcessFactory {

	private static Process process;

	public synchronized static Process getRootProcess(boolean isNew) throws IOException {
		if (isNew || process == null) {
			try {
				if(process != null)
				{
					destroy();
				}
				process = TerminalService.execute("su", false);
			} catch (IOException e) {
				throw e;
			}
		}
		return process;
	}
	
	public synchronized static boolean destroy() throws IOException
	{
		if(process == null)
			return true;
		process.getOutputStream().close();
		process.destroy();
		process = null;
		return true;
	}
	
}
